<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1500" destroy-on-close @closed="$emit('closed')"
             align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="170" label-position="right">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item :label="$t('sale.sale.sale')" prop="contract_id">
                        <el-input v-model="contract_code" :placeholder="$t('all.select') + ' ' + $t('sale.sale.sale')" @click="checkContract" readonly></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('production.code')" prop="production_code">
                        <el-input v-model="form.production_code" :placeholder="$t('all.auto')" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('sale.sale.date')" prop="open_date">
                        <el-date-picker v-model="form.open_date" type="date" :placeholder="$t('all.select')" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item :label="$t('production.delivery')" prop="over_date">
                        <el-date-picker v-model="form.over_date" type="date" :placeholder="$t('all.select')" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="6">
                <el-form-item :label="$t('production.priority')" prop="level">
                    <el-select v-model="form.level" :placeholder="$t('all.select')" style="width:100%" clearable>
                        <el-option :label="$t('production.normal')" :value="1"/>
                        <el-option :label="$t('production.urgent')" :value="2"/>
                        <el-option :label="$t('production.extension')" :value="3"/>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-form-item :label="$t('all.attachment')" prop="enclosure">
                <sc-upload-file v-model="form.enclosure" :limit="1">
                    <el-button type="primary" icon="el-icon-upload">{{$t('all.selectattachment')}}</el-button>
                </sc-upload-file>
            </el-form-item>
            <el-col :span="12">
                <el-form-item :label="$t('all.remark')" prop="remark">
                    <el-input v-model="form.remark" :placeholder="$t('all.remark')" type="textarea" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item :label="$t('procure.plan.product')" prop="detail">
                    <el-button type="primary" size="small" icon="el-icon-plus" @click="checkGoods">{{$t ('goods.bom.select')}}</el-button>
                    <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" :placeholder="$t('all.noData')" height="250" hideAdd>
                        <el-table-column prop="goods_code" :label="$t('goods.goods.code')" align="center">
                            <template #default="scope">
                                {{scope.row.goods_code}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_name" :label="$t('goods.goods.name')" align="center">
                            <template #default="scope">
                                {{scope.row.goods_name}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_model" :label="$t('goods.goods.model')" align="center">
                            <template #default="scope">
                                {{scope.row.goods_model}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="goods_spec" :label="$t('goods.goods.spec')" align="center">
                            <template #default="scope">
                                {{scope.row.goods_spec}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="unit_name" :label="$t('goods.unit.unit')" align="center">
                            <template #default="scope">
                                {{scope.row.unit_name}}
                            </template>
                        </el-table-column>
                        <el-table-column :label="$t('production.productnum')" prop="number" align="center">
                            <template #default="scope">
                                <el-input v-model="scope.row.number" :placeholder="$t('production.productnum')" @focus="onFocus($event)"></el-input>
                            </template>
                        </el-table-column>
                    </sc-form-table>
                </el-form-item>
            </el-col>
        </el-form>
		<template #footer>
			<el-button @click="visible = false">{{$t('all.cancel')}}</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">{{$t('all.save')}}</el-button>
		</template>
	</el-dialog>
    <select-goods v-if="dialog.goods" ref="goodsDialog" @success="goodsUpdate" @closed="dialog.goods=false" :query="{system_disable:0}"></select-goods><!-- 产品 -->
    <select-contract v-if="dialog.contract" ref="contractDialog" @success="contractUpdate" @closed="dialog.contract=false" :query="{status:1}"></select-contract><!-- 销售 -->
</template>

<script>
import SelectContract from '../../sale/contract/selectContract.vue';
import selectGoods from '../../goods/info/selectGoods.vue';
export default {
    components: { selectGoods, SelectContract },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
                add: this.$t('all.add'),
                mrpadd: this.$t('all.add'),
                production: this.$t('all.add'),
                edit: this.$t('all.edit'),
                show: this.$t('all.show')
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
                id: "",
                production_code: "",
                contract_id: "",
                open_date: this.moment().format('YYYY-MM-DD'),
                over_date: "",
                level: 1,
                enclosure: "",
                remark: "",
                detail: [],
			},
			//验证规则
			// rules: {
			// 	contract_id: [{ required: true, message: '请选择销售' }],
			// },
            contract_code: "",
            addTemplate:{},
            dialog:{
                goods: false,
                contract: false,
            },
            disabled: false
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.production.plan.edit.post(this.form);
					} else {
						res = await this.$API.production.plan.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success(this.$t('all.yes'))
					} else {
						this.$alert(res.message, this.$t('all.prompt'), {type: "error"})
					}
				} else {
					return false;
				}
			})
		},
        goProduction(data){
            this.disabled = true
            this.form.contract_id = data.id
            this.contract_code = data.contract_code
            let array = []
            data.product.map(item=>{
                array.push({
                    goods_id:item.goods_id,
                    goods_name:item.goods && item.goods.goods_name,
                    goods_spec:item.goods && item.goods.goods_spec,
                    goods_model:item.goods && item.goods.goods_model,
                    unit_name:item.goods && item.goods.unit && item.goods.unit.unit_name,
                    goods_code:item.goods && item.goods.goods_code,
                })
                this.form.detail = array
            })
        },
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.production.plan.detail.get({id:data.id});
            this.form.production_code = data.production_code
            this.form.contract_id = data.contract_id
            this.contract_code = data.contract&&data.contract.contract_code
            this.form.open_date = data.open_date
            this.form.over_date = data.over_date
            this.form.level = data.level
            this.form.enclosure = data.enclosure
            this.form.remark = data.remark
            let array = []
            res.data.detail.map(item=>{
                array.push({
                    id:item.id,
                    goods_id:item.goods_id,
                    goods_code:item.goods && item.goods.goods_code,
                    goods_name:item.goods && item.goods.goods_name,
                    goods_spec:item.goods && item.goods.goods_spec,
                    goods_model:item.goods && item.goods.goods_model,
                    unit_name:item.goods && item.goods.unit && item.goods.unit.unit_name,
                    number:item.number,

                })
            })
            this.form.detail = array
		},
        createProduct(data){
            console.log('data :>> ', data);
            this.form.contract_id = data.contract_id
            this.contract_code = data.contract_code
            let array = []
            data.data.map(item=>{
                array.push({
                    goods_id:item.goods_id,
                    goods_name:item.goods_name,
                    unit_name:item.unit_name,
                    goods_code:item.goods_code,
                    goods_model:item.goods_model,
                    goods_spec:item.goods_spec,
                    number:item.number,
                })
            })
            this.form.detail = array
        },
        onFocus(event){
            event.target.select();
        },
        checkContract(){
            if(!this.disabled){
                this.dialog.contract = true
                this.$nextTick(() => {
                    this.$refs.contractDialog.open('add')
                })
            }
        },
        contractUpdate(data){
            this.form.contract_id = data.id
            this.contract_code = data.contract_code
            let array = []
            data.product.map(item=>{
                array.push({
                    goods_id:item.goods_id,
                    goods_name:item.goods.goods_name,
                    goods_spec:item.goods.goods_spec,
                    goods_model:item.goods.goods_model,
                    unit_name:item.goods.unit_name,
                    goods_code:item.goods.goods_code,
                })
                this.form.detail = array
            })
        },
        // 选择商品
        checkGoods(e){
            this.dialog.goods = true
            this.$nextTick(() => {
                this.$refs.goodsDialog.open('add').setData(e)
            })
        },
        goodsUpdate(data){
            let array = []
            data.map((item)=>{
                array.push({
                    goods_id:item.id,
                    goods_name:item.goods_name,
                    goods_spec:item.goods_spec,
                    goods_model:item.goods_model,
                    unit_name:item.unit_name,
                    goods_code:item.goods_code,
                })
            })
            let existingIds = this.form.detail.map(item => item.goods_id);
            for(let i = array.length - 1; i >= 0; i--){
                if(existingIds.includes(array[i].goods_id)){
                    array.splice(i, 1);
                }
            }
            this.form.detail = [...this.form.detail,...array]
        },
	}
}
</script>

<style></style>
